package entry;

import frame.laxcen.BaseFrame;
import frame.laxcen.request.Request_0010_10;
import frame.laxcen.request.Request_0010_11;
import frame.laxcen.request.Request_0010_FF;
import frame.laxcen.response.Response_0010_10;
import frame.laxcen.response.Response_0010_11;
import frame.laxcen.util.FrameUtil;
import tcpclient.v2.RFIDClient;
import util.FlowUtil;
import util.Ref;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.List;

public class EntryV2 {

    public static void test1() throws IOException {

        RFIDClient client = new RFIDClient(1 << 20, 1024);

        if(client.tryConnect(new InetSocketAddress("192.168.1.116", 9090), 3000)) {

            System.out.println("连接成功！");

            Request_0010_10.RequestTemplate_0010_10 template = new Request_0010_10.RequestTemplate_0010_10();

            template.ReadMode = (byte) 0x01;

            template.Pid_0x02_0 = (byte) 0x01;

            template.Pid_0x02_1 = (byte) 0x06;

            template.Pid_0x03_0 = (byte) 0x00;

            template.Pid_0x03_1 = (byte) 0x00;

            template.Pid_0x03_2 = (byte) 0x6;

            if(client.trySend(new Request_0010_10(template), 3000)) {

                System.out.println("发送循环读命令 ...");
            }

            System.out.println("> 第一次读取 -------------------------------------------------------------------");

            test1_getResponse(client);

            System.out.println("> 第二次读取 -------------------------------------------------------------------");

            test1_getResponse(client);

            System.out.println("> 第三次读取 -------------------------------------------------------------------");

            test1_getResponse(client);

            if(client.trySend(new Request_0010_FF(), 3000)) {

                System.out.println("发送停止命令 ...");

                System.out.println("> 第四次读取 -------------------------------------------------------------------");

                test1_getResponse(client);

                System.out.println("> 第五次读取 -------------------------------------------------------------------");

                test1_getResponse(client);

                System.out.println("> 第六次读取 -------------------------------------------------------------------");

                test1_getResponse(client);
            }
        }

        if(client.tryClose()) System.out.println("成功关闭连接！"); else System.out.println("没有成功关闭！");
    }

    public static void test1_getResponse(RFIDClient client) {

        Ref<List<BaseFrame>> upload = Ref.empty(), notUpload = Ref.empty();

        client.waitForResponseListSplitByUploadFlag(1000, notUpload, upload);

        System.out.println("> Not Upload");

        notUpload.ifPresent((frames) -> frames.forEach((frame) -> FlowUtil.silent(frame, (f) -> FrameUtil.printResponse(new Response_0010_10(frame)))));

        System.out.println("> Upload");

        upload.ifPresent((frames) -> frames.forEach((frame) -> FlowUtil.silent(frame, (f) -> FrameUtil.printResponse(new Response_0010_10(frame)))));
    }

    public static void test2() throws IOException {

        RFIDClient client = new RFIDClient(1 << 20, 1024);

        if(client.tryConnect(new InetSocketAddress("192.168.1.116", 9090), 3000)) {

            System.out.println("连接成功！");

            if(client.trySend(new Request_0010_11(
                    new byte[] { (byte) 0xE2, (byte) 0x80, (byte) 0x11, (byte) 0x05, (byte) 0x20, (byte) 0x00, (byte) 0x74, (byte) 0x9B, (byte) 0xBC, (byte) 0xF5, (byte) 0x09, (byte) 0x19 },
                    new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff }), 3000)) {

                System.out.println("写标签 ...");

                client.waitForArbitraryResponse(3000).ifPresent((frame) -> FlowUtil.silent(frame, (f) -> FrameUtil.printResponse(new Response_0010_11(frame))));

                System.out.println("操作结束");
            }
        }

        if(client.tryClose()) System.out.println("成功关闭连接！"); else System.out.println("没有成功关闭！");
    }

    public static void main(String[] args) throws IOException {

        test1();
    }
}
